package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;


public class ContactHelper extends WebDriverHelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;


	public ContactHelper(ApplicationManager manager) {
		super(manager);
		
	}
	
	public ContactHelper createContact(ContactData contact) {
		manager.navigateTo().homePage();
		initContactCreation();
    	fillContactForm(contact, CREATION);
    	submitNewContact();
		goToHomePage();
		manager.getModel().addContact(contact);
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		manager.navigateTo().homePage();
		editContact(index);
    	fillContactForm(contact, MODIFICATION);
    	updateContact();
    	goToHomePage();
    	manager.getModel().removeContact(index).addContact(contact);
    	return this;		
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().homePage();
    	editContact(index);
    	deleteContact();
    	goToHomePage();
    	manager.getModel().removeContact(index);
    	return this;			
	}
	
	
		
	// ------------------------------------------------------------------------

	public SortedListOf<ContactData> getUiContacts() {
		SortedListOf<ContactData> contacts = new SortedListOf<ContactData>();
		
        manager.navigateTo().homePage();
        List<WebElement> rows =  driver.findElements(By.xpath("//tr[@name='entry']"));
        for (WebElement row : rows) {
                List<WebElement> cells = row.findElements(By.tagName("td"));
                String lastName = cells.get(1).getText();
                contacts.add(new ContactData().withLastName(lastName));
        }
        return contacts;
}

	
	public ContactHelper editContact(int index) {
		clickContactByIndex(index);
		return this;
	}
	
	public ContactHelper deleteContact() {
		click(By.xpath("//div[@id='content']/form[2]/input[2]"));
		return this;
	}
	
	public ContactHelper initContactCreation() {
		click(By.linkText("add new"));
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAddress1());
		type(By.name("home"), contact.getHomePhone());
		type(By.name("mobile"), contact.getMobilePhone());
		type(By.name("work"), contact.getWorkPhone());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
	    selectByValue(By.name("bday"), contact.getBday());
	    selectByValue(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		if (formType == CREATION){
			//selectByValue(By.name("new_group"), contact.group);
	    } else {
	    	if (driver.findElements(By.name("new_group")).size() !=0){
	    		throw new Error("Group selector exists in contact modification form");
	    	}
	    }
		type(By.name("address2"), contact.getAddress2());
	    type(By.name("phone2"), contact.getPhone2());
	    
	    return this;
	  	}
	
	public ContactHelper submitNewContact() {
		click(By.name("submit"));
		return this;
	}

		
	private void clickContactByIndex(int index) {
		click(By.xpath("//tr["+ (index+2) +"]/td[7]/a/img"));
	}

	
	public ContactHelper updateContact() {
		click(By.xpath("//div[@id='content']/form[1]/input[11]"));
		return this;		
	}
	
	public void goToHomePage() {
		click(By.linkText("home"));
		}
	}


